package ru.vms.healthcontrol;

import android.content.SharedPreferences;
import android.provider.ContactsContract.CommonDataKinds.Email;

public class UserData {

	public static final String UNDEFINED_STR = "???";
	
	protected String login;
	protected String password;
	protected String nick;
	protected boolean isLoginComplete = false;
	
	protected int age = -1;
	protected int height = -1;
	protected int sex = -1;


	protected UserData ( String lg, String ps, String nk ) {
		this.login = lg;
		this.password = ps;
		this.nick = nk;
	}
	
	static UserData loadCurrentUserData (SharedPreferences prefs) {
		
    	String sex = prefs.getString("sex", UNDEFINED_STR);
    	String age = prefs.getString("age", UNDEFINED_STR);
    	String height = prefs.getString("height", UNDEFINED_STR);

    	String email = prefs.getString("email", UNDEFINED_STR);
    	String password = prefs.getString("password", UNDEFINED_STR);
    	String nick = prefs.getString("nick", UNDEFINED_STR);
    	boolean loginComplete = prefs.getBoolean("signin_complete", false);

		UserData u = new UserData(email.length()<=0?UNDEFINED_STR:email,
				password.length()<=0?UNDEFINED_STR:password,
				nick.length()<=0?UNDEFINED_STR:nick);
		
		u.age = parseInt(age);
		u.sex = parseInt(sex);
		u.isLoginComplete = loginComplete;
		u.height = parseInt(height);
		
		return u;
	}
	
	static private int parseInt ( String str ) {
		return str.equals(UNDEFINED_STR)? -1 : Integer.parseInt(str);
	}


	public String getLogin() {
		return login;
	}


	public String getPassword() {
		return password;
	}


	public String getNick() {
		return nick;
	}
	
	
	public boolean isLoginComplete() {
		return isLoginComplete;
	}

	public int getAge() {
		return age;
	}

	public int getHeiht() {
		return height;
	}

	public int getSex() {
		return sex;
	}
	
	public String getAgeStr() {
		return (age <= 0)? UNDEFINED_STR : Integer.toString(age);
	}

	public String getHeihtStr() {
		return (height <= 0)? UNDEFINED_STR : Integer.toString(height);
	}

	public String getSexStr() {
		return (sex < 0)? UNDEFINED_STR : (sex>= 1)? "�":"�";
	}

	public static UserData getDefault() {
		UserData u = new UserData(UNDEFINED_STR, UNDEFINED_STR, UNDEFINED_STR);
		return u;
	}

	public boolean isReadyToSignIn() {
		
		return !( login.equals(UNDEFINED_STR) || password.equals(UNDEFINED_STR) || nick.equals(UNDEFINED_STR) 
				|| login.length() <= 0 || password.length() <= 0 || nick.length() <= 0 );
	}
	
}
